<template>
  <!-- 资讯分类管理 -->
  <div>
    <div class="header">
      <el-button type="success" icon="el-icon-plus" @click="add_topic" circle></el-button>
    </div>
    <div class="content">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="70">
        </el-table-column>
        <el-table-column prop="name" label="名称" width="150">
        </el-table-column>
        <el-table-column type="index" label="排序号" width="70">
        </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>

        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button type="text" size="small" style="color:red" @click="delete_topic(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      @close="dialogClose"
    >
      <!-- 表单区域 -->
      <!-- :model 表单对象 最终我们要提交的那个表单对象 -->
      <!-- :rules 表单验证的规则 -->
      <!-- ref 类比与 id 最终通过ref找到这张表单 -->
      <el-form
        :model="topicForm"
        :rules="rules"
        ref="topic_form"
        label-width="80px"
      >
        <!-- 产品名称 -->
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="topicForm.name"></el-input>
        </el-form-item>
        <el-form-item label="序号" prop="no">
          <el-input v-model.number="topicForm.no"></el-input>
        </el-form-item>
        <!-- 封面 -->
        <el-form-item label="栏目介绍" prop="description">
          <el-input
            type="textarea"
            v-model="topicForm.description"
          ></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部操作按钮区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogClose">取 消</el-button>
        <!-- 提交表单后，调用重置表单的方法 -->
        <el-button type="primary" @click="submitMForm"> 确 定 </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { get, post } from "@/utils/request";
export default {
  data() {
    return {
      tableData: [],
      dialogTitle:'',
      dialogVisible: false,
      formLabelWidth: '100px',
      topicForm:{
        name: '',
        no:'',
        // 产品描述
        description: '',
      },
      rules:{
        name:[{required: true, message: '请填写栏目名称', trigger: 'blur'}],
        no:[{ type: "number", message: "序号必须是数字" }],
        description:[],
      }
      
    };
  },
  computed: {},
  methods: {
    add_topic(){
      this.dialogVisible = true;
      this.topicForm = {};
      this.dialogTitle = "添加栏目信息"
    },
    handleClick(row) {
      this.topicForm = { ...row };
      this.dialogVisible = true;
      // console.log(row);
      this.dialogTitle = "编辑栏目信息"
    },
    async getCategory() {
      // let params;
      let res = await get("/category/findAll");
      this.tableData = res.data;
      console.log(res);
    },
    
    async delete_topic(id){
      let res = await get('/category/deleteById',{id});
      console.log(res);
      if (res.status == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
        this.getCategory();
      } else {
        this.$message.error(res.message);
      }
    },
    submitMForm(){
      this.$refs['topic_form'].validate(async (valid) => {
        if (valid) {
          // 发送表单验证的请求 提交表单对象
          let res = await post('/category/saveOrUpdate', this.topicForm)
          // 给用户一个成功的提示
          if (res.status == 200) {
            this.$message({
              message: res.message,
              type: "success",
            });
            // 重新刷新页面的数据
            this.getCategory();
            // 调用关闭模态框的方法
            this.dialogClose()
          } else {
            this.$message.error(res.message);
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    dialogClose(){
      // 重置表单
      this.$refs['topic_form'].resetFields();
      // 关闭模态框
      setTimeout(() => {
        this.dialogVisible = false
      }, 200);
    }
  },
  created() {
    this.getCategory();
  },
  mounted() {},
};
</script>
<style scoped>
</style>